var dg; //角色datagrid
var d; //弹窗
var permissionDg;   //权限datagrid
var rolePerData;    //用户拥有的权限
var roleId; //双击选中的role
$(function(){   
    dg=$('#dg').datagrid({    
    method: "get",
    url:'../../api/nj_getAllRoleInfoBypage', 
    //fit : true,
    //fitColumns : true,
    //border : false,
    width: 700,
    idField : 'id',
    pagination:true,
    rownumbers:true,
    pageNumber:1,
    pageSize : 10,
    pageList : [ 10, 20, 30, 40, 50 ],
    singleSelect:true,
    striped:true,
    columns:[[    
        {field:'id',title:'id',hidden:true,sortable:true,width:100},    
        {field:'roleode',title:'角色编码',sortable:true,width:200},
        {field:'rolename',title:'角色名称',sortable:true,width:200,tooltip: true},
        {field : 'action',title : '操作',width:100,
            formatter : function(value, row, index) {
                return '<a href="javascript:lookP('+row.id+')"><div class="icon-lock" style="width:16px;height:16px" title="查看权限"></div></a>';
            }
        }
    ]],
    enableHeaderClickMenu: false,
    enableHeaderContextMenu: false,
    enableRowContextMenu: false,
    toolbar:'#tb'
    });
    
    permissionDg=$('#permissionDg').treegrid({   
        method: "get",
        url:'../../api/nj_getAllAuthorityInfo_Treegrid', 
        //fit : true,
        //fitColumns : true,
        //border : false,
        width: 500,
        idField : 'id',
        treeField:'name',
        parentField : 'parentid',
        iconCls: 'icon',
        animate:true, 
        rownumbers:true,
        striped:true,
        singleSelect:false,//需设置  
        columns:[[    
            {field:'ck',checkbox:true,hidden:true,width:100},   
            {field:'id',title:'id',hidden:true,width:100},    
            {field:'authorityname',title:'权限名称',width:200},
            {field:'authority',title:'权限描述',width:200,tooltip: true}
        ]],
        onClickRow:function(row){  
            //级联选择  
            $(this).treegrid('cascadeCheck',{  
                id:row.id, //节点ID  
                deepCascade:true //深度级联  
            });  
        },
        toolbar:'#tg_tb'
        });
});

//查看权限
function lookP(roleId){
    //清空勾选的权限
    if(rolePerData){
        permissionDg.treegrid('unselectAll');
        rolePerData=[];//清空
    }
    //获取角色拥有权限
    $.ajax({
        async:false,
        type:'get',
        url:"../../api/NJ_GetAllJoinRoleAuthorityByroleid?roleid="+roleId,//根据角色id查询所有的权限
        success: function(data){
            if(typeof data=='object'){
                rolePerData=data;
                for(var i=0,j=data.length;i<j;i++){
                    permissionDg.treegrid('select',data[i]);
                }
            }else{
                $.easyui.messager.alert(data);
            } 
        }
    });
}

//保存修改权限
function save(){
    var row = dg.datagrid('getSelected');
    var roleId=row.id;
    parent.$.messager.confirm('提示', '确认要保存修改？', function(data){
    if (data){
        var newPermissionList=[];
        var data=permissionDg.treegrid('getSelections');
        for(var i=0,j=data.length;i<j;i++){
            newPermissionList.push(data[i].id);
        }
        
        if(roleId==null) {
            parent.$.messager.show({ title : "提示",msg: "请选择角色！", position: "bottomRight" });
            return;
        }
        $.ajax({
            async:false,
            type:'POST',
            data:JSON.stringify(newPermissionList),
            contentType:'application/json;charset=utf-8',
            url:"${ctx}/system/role/"+roleId+"/updatePermission",
            success: function(data){
                successTip(data);
            }
        });
    } 
    });
}

//弹窗增加
function add() {
    $.ajaxSetup({type : 'GET'});
    d=$('#dlg').dialog({    
        title: '添加角色',    
        width: 400,    
        height: 260,    
        closed: false,    
        cache: false,
        maximizable:true,
        resizable:true,
        href: '${ctx}/system/role/create',
        modal: true,
        buttons:[{
            text:'确认',
            handler:function(){
                $("#mainform").submit();
            }
        },{
            text:'取消',
            handler:function(){
                    d.panel('close');
                }
        }]
    });
}

//删除
function del(){
    var row = dg.datagrid('getSelected');
    if(rowIsNull(row)) return;
    parent.$.messager.confirm('提示', '删除后无法恢复您确定要删除？', function(data){
        if (data){
            $.ajax({
                type:'get',
                url:"${ctx}/system/role/delete/"+row.id,
                success: function(data){
                    successTip(data,dg);
                }
            });
            //dg.datagrid('reload'); //grid移除一行,不需要再刷新
        } 
    });
}

//修改
function upd(){
    var row = dg.datagrid('getSelected');
    if(rowIsNull(row)) return;
    var rowIndex = row.id;
    $.ajaxSetup({type : 'GET'});
    d=$("#dlg").dialog({   
        title: '修改角色',    
        width: 400,    
        height: 260,      
        href: '${ctx}/system/role/update/'+rowIndex,
        maximizable:true,
        modal:true,
        buttons:[{
            text:'确认',
            handler:function(){
                $("#mainform").submit(); 
            }
        },{
            text:'取消',
            handler:function(){
                    d.panel('close');
                }
        }]
    });
}

//恢复权限选择
function back(){
    var row = dg.datagrid('getSelected');
    lookP(row.id);
}